package com.ht.pms.core.dao.impl;

import com.ht.pms.core.common.constant.TemplateEnum;
import com.ht.pms.core.dao.DataImportDao;
import com.ht.pms.core.dto.DataImportQueryReq;
import com.ht.pms.core.dto.upload.UpLoadTemp;
import com.ht.pms.core.dto.upload.UploadFile;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author:喻尊龙
 * @date: 2018/4/19
 */
@Service
public class DataImportDaoImpl implements DataImportDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<UpLoadTemp> getUpLoadTempList(String tableName,int offSet,int size) {
        String sql = "SELECT * FROM t_upload_temp t1 WHERE t1.del=0 ";
        if(StringUtils.isNotBlank(tableName)){
            sql = sql + " and t1.file_name like '%"+tableName+"%'";
        }
        sql = sql +" ORDER BY last_update_time DESC LIMIT "+offSet+","+size+"";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper(UpLoadTemp.class));
    }

    @Override
    public int getUpLoadTempListCount(String tableName) {
        String sql = "SELECT count(*) as count FROM t_upload_temp t1 WHERE t1.del=0 ";
        if(StringUtils.isNotBlank(tableName)){
            sql = sql + " and t1.file_name like '%"+tableName+"%'";
        }
        Map<String,Object> map = jdbcTemplate.queryForMap(sql);
        int count = 0;
        if(map != null){
            count = Integer.parseInt(map.get("count")+"");
        }
        return count;
    }

    @Override
    public int deleteUploadTemp(String id) {
        String sql = "DELETE from t_upload_temp  WHERE id='"+id+"'";
        return jdbcTemplate.update(sql);
    }

    @Override
    public int deleteUploadFile(String fileId, String batchCode) {
        String sql = "UPDATE t_upload_file set is_del='1' WHERE file_id='"+fileId+"' and batch_code='"+batchCode+"'";
        return jdbcTemplate.update(sql);
    }

    @Override
    public int deleteData(String fileId, String batchCode) {
        String sqla = "UPDATE t_dict_record_a set in_state=0 WHERE file_id="+fileId+" and batch_code='"+batchCode+"'";

        String sqlb = "UPDATE t_dict_record_b set in_state=0 WHERE file_id="+fileId+" and batch_code='"+batchCode+"'";

        String sqlc = "UPDATE t_dict_record_c set in_state=0 WHERE file_id="+fileId+" and batch_code='"+batchCode+"'";
        jdbcTemplate.update(sqla);
        jdbcTemplate.update(sqlb);
        jdbcTemplate.update(sqlc);
        return 0;
    }

    @Override
    public int updataBase(String batchCode) {
        String sql="UPDATE t_dict_record_base  set in_state='0' WHERE batch_code='"+batchCode+"'";
        return jdbcTemplate.update(sql);
    }

    @Override
    public UpLoadTemp getNewUpLoadTemp() {
        String sql="SELECT * FROM t_upload_temp  WHERE del='0' ORDER BY create_time DESC LIMIT 0,1";
        UpLoadTemp upLoadTemp = null;
        try{
            upLoadTemp = (UpLoadTemp)jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(UpLoadTemp.class));
        }catch (EmptyResultDataAccessException e){

        }
        return upLoadTemp;
    }

    @Override
    public UploadFile getNewUploadFile(String batchCode) {
        String sql="SELECT * FROM t_upload_file WHERE batch_code='"+batchCode+"' ORDER BY create_time DESC LIMIT 0,1";
        UploadFile uploadFile = null;
        try{
            uploadFile = (UploadFile)jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(UploadFile.class));
        }catch (EmptyResultDataAccessException e){

        }
        return uploadFile;
    }

    @Override
    public List<UploadFile> getUploadFileList(DataImportQueryReq req) {
        String sql = "SELECT * from t_upload_file t1 WHERE 1=1 ";
        if(StringUtils.isNotBlank(req.getTableName())){
            sql = sql + " AND t1.file_name like '%"+req.getTableName()+"%'";
        }
        sql = sql +" AND t1.is_del='0' ORDER BY t1.create_time DESC LIMIT "+(req.getPage()-1)*req.getLimit()+","+req.getLimit()+"";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper(UploadFile.class));
    }

    @Override
    public int getUploadFileListCount(DataImportQueryReq req) {

        String sql = "SELECT count(*) as count from t_upload_file t1 WHERE 1=1 ";
        if(StringUtils.isNotBlank(req.getTableName())){
            sql = sql + " AND t1.file_name like '%"+req.getTableName()+"%'";
        }
        sql = sql +" AND t1.is_del='0' ";
        Map<String,Object> map = jdbcTemplate.queryForMap(sql);
        int count = 0;
        if(map != null){
            count = Integer.parseInt(map.get("count")+"");
        }
        return count;
    }

    @Override
    public int getFailImportCount(String fileId,String batchCode) {
        String sql = "SELECT count(*) as count from t_dict_record_base base WHERE file_id='"+fileId+"' and batch_code='"+batchCode+"' and in_state='0'";
        Map<String,Object> map = jdbcTemplate.queryForMap(sql);
        int count = 0;
        if(map != null){
            count = Integer.parseInt(map.get("count")+"");
        }
        return count;
    }

    @Override
    public int updataCheckState(String fileId, String batchCode) {
        String sql = "UPDATE t_upload_file set state='1' WHERE file_id='"+fileId+"' and batch_code='"+batchCode+"'";
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updataRecordCheckState(String fileId, String batchCode, String templatCode) {
        String sql = "";
        if(TemplateEnum.TEMP_XZ.getCode().equals(templatCode)){
            sql = "UPDATE t_dict_record_b SET audit_state=1 WHERE batch_code='"+batchCode+"' AND file_id='"+fileId+"'";
        }
        if(TemplateEnum.TEMP_JC.getCode().equals(templatCode)){
            sql = "UPDATE t_dict_record_c SET audit_state=1 WHERE batch_code='"+batchCode+"' AND file_id='"+fileId+"'";
        }
        if(TemplateEnum.TEMP_KQ.getCode().equals(templatCode)){
            sql = "UPDATE t_dict_record_a SET audit_state=1 WHERE batch_code='"+batchCode+"' AND file_id='"+fileId+"'";
        }
        return jdbcTemplate.update(sql);
    }
}
